<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" >
      <el-form-item label="问卷名称" prop="surveyName">
        <el-input style="width: 200px" v-model="queryParams.surveyName" placeholder="请输入问卷名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="问卷类型" prop="surveyType">
        <el-select style="width: 200px" v-model="queryParams.surveyType" placeholder="请选择问卷类型" clearable>
          <el-option v-for="dict in dict.survey_type" :key="dict.dictValue" :label="dict.dictName" :value="dict.dictValue" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="surveyList" stripe table-layout="fixed">
      <el-table-column label="问卷名称" prop="surveyName" />
      <el-table-column label="问卷描述" prop="surveyDesc" />
      <el-table-column label="问卷类型" prop="surveyType">
        <template #default="scope">
          <template v-for="item in dict.survey_type" :key="item.dictValue">
            <el-tag v-if="item.dictValue == scope.row.surveyType" :type="item.cssClass">
              {{ item.dictName }}
            </el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="采集人数" prop="userNum" align="center" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="small" type="primary" text icon="View" @click="handleDetail(scope.row)" >数据详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 10px"
      size="small" 
      layout="prev, pager, next, jumper, ->, total"
      :total="total"
      :page-size="queryParams.pageSize"
      :current-page="queryParams.pageNum"
      @current-change="getList"
    />
  </div>
</template>

<script>
import {
  analysisList,
} from "@/api/survey/survey";
import { getDicts } from "@/api/dict";

export default {
  name: "Survey",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 问卷表格数据
      surveyList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        surveyName: null,
        surveyDesc: null,
        surveyType: null,
        surveyStatus: "1,2",
        status: "1",
        isTemplate: '0',
      },
      dict: {
        survey_type: [],
        survey_status: [],
      },
    };
  },
  async created() {
    this.getList();
    this.dict.survey_status = await getDicts("survey_status");
    this.dict.survey_type = await getDicts("survey_type");
  },
  methods: {
    /** 查询问卷列表 */
    getList() {
      this.loading = true;
      analysisList(this.queryParams).then((response) => {
        this.surveyList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    handleDetail(row) {
      this.$router.push({ path: `/analysis/detail/${row.surveyId}` });
    }
  },
};
</script>

<style scoped>
.el-button.el-button--small.is-text{
  padding: 0 4px;
  margin: 0;
}
.showcase {
  font-size: 12px;
  vertical-align: middle;
  margin-left: 10px;
}
.showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
</style>
